<?php
/**
* @file $Id: EnterEligibility.php 437 2007-04-23 00:57:51Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

$start_end_RET = DBGet(DBQuery("SELECT TITLE,VALUE FROM PROGRAM_CONFIG WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' AND PROGRAM='eligibility'"));
if(count($start_end_RET))
{
	foreach($start_end_RET as $value)
		$$value['TITLE'] = $value['VALUE'];
}

switch(date('D'))
{
	case 'Mon':
	$today = 1;
	break;
	case 'Tue':
	$today = 2;
	break;
	case 'Wed':
	$today = 3;
	break;
	case 'Thu':
	$today = 4;
	break;
	case 'Fri':
	$today = 5;
	break;
	case 'Sat':
	$today = 6;
	break;
	case 'Sun':
	$today = 7;
	break;
}

$days = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

if(strlen($START_MINUTE)==1)
	$START_MIN = '0'.$START_MINUTE;
if(strlen($END_MINUTE)==1)
	$END_MINUTE = '0'.$END_MINUTE;

$start_date = strtoupper(date('d-M-y',mktime()-($today-$START_DAY)*60*60*24));
$end_date = strtoupper(date('d-M-y',mktime()+($END_DAY-$today)*60*60*24));

$current_RET = DBGet(DBQuery("SELECT ELIGIBILITY_CODE,STUDENT_ID FROM ELIGIBILITY WHERE SCHOOL_DATE BETWEEN '$start_date' AND '$end_date' AND COURSE_PERIOD_ID='".UserCoursePeriod()."'"),array(),array('STUDENT_ID'));

if($_REQUEST['modfunc']=='gradebook')
{
	$config_RET = DBGet(DBQuery("SELECT TITLE,VALUE FROM PROGRAM_USER_CONFIG WHERE USERNAME='".User('USERNAME')."' AND PROGRAM='Gradebook'"),array(),array('TITLE'));
	if(count($config_RET))
	{
		foreach($config_RET as $title=>$value)
			$programconfig[$title] = $value[1]['VALUE'];
	}
	include 'ProgramFunctions/_makeLetterGrade.fnc.php';

	$course_period_id = UserCoursePeriod();
	$course_id = DBGet(DBQuery("SELECT COURSE_ID FROM COURSE_PERIODS WHERE COURSE_PERIOD_ID='".UserCoursePeriod()."'"));
	$course_id = $course_id[1]['COURSE_ID'];

	$grades_RET = DBGet(DBQuery("SELECT ID,TITLE,GPA_VALUE FROM REPORT_CARD_GRADES WHERE SCHOOL_ID='".UserSchool()."' AND SYEAR='".UserSyear()."' ORDER BY SORT_ORDER"),array(),array('TITLE'));

	if($programconfig['ELIGIBILITY_CUMULITIVE']=='Y')
		$mp_list = GetChildrenMP('SEM',UserMP());
	else
		$mp_list = GetAllMP('QTR',UserMP());

	if($programconfig['WEIGHT']=='Y')
		$points_RET = DBGet(DBQuery("SELECT gt.ASSIGNMENT_TYPE_ID,gg.STUDENT_ID,(sum(".db_case(array('gg.POINTS',"'-1'","'0'",'gg.POINTS')).") * gt.FINAL_GRADE_PERCENT) AS DIVIDEND, sum(".db_case(array('gg.POINTS',"'-1'","'0'",'ga.POINTS')).") AS DIVISOR FROM GRADEBOOK_GRADES gg, GRADEBOOK_ASSIGNMENTS ga, GRADEBOOK_ASSIGNMENT_TYPES gt WHERE gt.ASSIGNMENT_TYPE_ID=ga.ASSIGNMENT_TYPE_ID AND ga.ASSIGNMENT_ID=gg.ASSIGNMENT_ID AND ga.MARKING_PERIOD_ID IN (".$mp_list.") AND gg.COURSE_PERIOD_ID='$course_period_id' AND (ga.COURSE_PERIOD_ID='$course_period_id' OR ga.COURSE_ID='$course_id') GROUP BY gg.STUDENT_ID,gt.ASSIGNMENT_TYPE_ID,gt.FINAL_GRADE_PERCENT"),array(),array('STUDENT_ID','ASSIGNMENT_TYPE_ID'));
	else
		$points_RET = DBGet(DBQuery("SELECT '-1' AS ASSIGNMENT_TYPE_ID,gg.STUDENT_ID,(sum(".db_case(array('gg.POINTS',"'-1'","'0'",'gg.POINTS')).")) AS DIVIDEND, sum(".db_case(array('gg.POINTS',"'-1'","'0'",'ga.POINTS')).") AS DIVISOR FROM GRADEBOOK_GRADES gg, GRADEBOOK_ASSIGNMENTS ga WHERE ga.ASSIGNMENT_ID=gg.ASSIGNMENT_ID AND ga.MARKING_PERIOD_ID IN (".$mp_list.") AND gg.COURSE_PERIOD_ID='$course_period_id' AND (ga.COURSE_PERIOD_ID='$course_period_id' OR ga.COURSE_ID='$course_id') GROUP BY gg.STUDENT_ID"),array(),array('STUDENT_ID','ASSIGNMENT_TYPE_ID'));

	if(count($points_RET))
	{
		foreach($points_RET as $student_id=>$types)
		{
			$total = 0;
			foreach($types as $type_id=>$type)
			{
				if($type[1]['DIVISOR']==0)
					$type[1]['PARTIAL_PERCENT'] = 0;
				else
					$type[1]['PARTIAL_PERCENT'] = $type[1]['DIVIDEND'] / $type[1]['DIVISOR'];

				$total += $type[1]['PARTIAL_PERCENT'];
			}
			
			$grade = $grades_RET[_makeLetterGrade($total,UserCoursePeriod())][1];
			if($grade['GPA_VALUE']=='0' || !$grade['GPA_VALUE'])
				$code = 'FAILING';
			elseif(strpos($grade['TITLE'],'D')!==false || $grade['GPA_VALUE']<2)
				$code = 'BORDERLINE';
			else
				$code = 'PASSING';
			
			if($current_RET[$student_id])
				$sql = "UPDATE ELIGIBILITY SET ELIGIBILITY_CODE='".$code."' WHERE SCHOOL_DATE BETWEEN '$start_date' AND '$end_date' AND COURSE_PERIOD_ID='".UserCoursePeriod()."' AND STUDENT_ID='".$student_id."'";
			else
				$sql = "INSERT INTO ELIGIBILITY (STUDENT_ID,SCHOOL_DATE,SYEAR,PERIOD_ID,COURSE_PERIOD_ID,ELIGIBILITY_CODE) values('$student_id','".DBDate()."','".UserSyear()."','".UserPeriod()."','".$course_period_id."','".$code."')";
			DBQuery($sql);
		}
		$current_RET = DBGet(DBQuery("SELECT ELIGIBILITY_CODE,STUDENT_ID FROM ELIGIBILITY WHERE SCHOOL_DATE BETWEEN '$start_date' AND '$end_date' AND COURSE_PERIOD_ID='".UserCoursePeriod()."'"),array(),array('STUDENT_ID'));
	}
}

if($_REQUEST['values'] && $_POST['values'])
{
	$course_period_id = UserCoursePeriod();
	foreach($_REQUEST['values'] as $student_id=>$value)
	{
		if($current_RET[$student_id])
			$sql = "UPDATE ELIGIBILITY SET ELIGIBILITY_CODE='".$value."' WHERE SCHOOL_DATE BETWEEN '$start_date' AND '$end_date' AND PERIOD_ID='".UserPeriod()."' AND STUDENT_ID='".$student_id."'";
		else
			$sql = "INSERT INTO ELIGIBILITY (STUDENT_ID,SCHOOL_DATE,SYEAR,PERIOD_ID,COURSE_PERIOD_ID,ELIGIBILITY_CODE) values('$student_id','".DBDate()."','".UserSyear()."','".UserPeriod()."','".$course_period_id."','".$value."')";
		DBQuery($sql);
	}
	$RET = DBGet(DBQuery("SELECT 'completed' AS COMPLETED FROM ELIGIBILITY_COMPLETED WHERE STAFF_ID='".User('STAFF_ID')."' AND SCHOOL_DATE BETWEEN '$start_date' AND '$end_date' AND PERIOD_ID='".UserPeriod()."'"));
	if(!count($RET))
		DBQuery("INSERT INTO ELIGIBILITY_COMPLETED (STAFF_ID,SCHOOL_DATE,PERIOD_ID) values('".User('STAFF_ID')."','".DBDate()."','".UserPeriod()."')");

	$current_RET = DBGet(DBQuery("SELECT ELIGIBILITY_CODE,STUDENT_ID FROM ELIGIBILITY WHERE SCHOOL_DATE BETWEEN '$start_date' AND '$end_date' AND PERIOD_ID='".UserPeriod()."'"),array(),array('STUDENT_ID'));
}

$extra['SELECT'] .= ",'' AS PASSING,'' AS BORDERLINE,'' AS FAILING,'' AS INCOMPLETE";
$extra['functions'] = array('PASSING'=>'makeRadio','BORDERLINE'=>'makeRadio','FAILING'=>'makeRadio','INCOMPLETE'=>'makeRadio');
$columns = array('PASSING'=>_('Passing'),'BORDERLINE'=>_('Borderline'),'FAILING'=>_('Failing'),'INCOMPLETE'=>_('Incomplete'));

$stu_RET = GetStuList($extra);

echo "<FORM ACTION=Modules.php?modname=$_REQUEST[modname] method=POST>";
DrawHeader(ProgramTitle());

if($START_DAY && $END_DAY && ($today>$END_DAY || $today<$START_DAY || ($today==$START_DAY && date('Gi')<($START_HOUR.$START_MINUTE)) || ($today==$END_DAY && date('Gi')>($END_HOUR.$END_MINUTE))))
{
	if($START_HOUR>12)
	{
		$START_HOUR-=12;
		$START_M = 'PM';
	}
	else
		$START_M = 'AM';

	if($END_HOUR>12)
	{
		$END_HOUR-=12;
		$END_M = 'PM';
	}
	else
		$END_M = 'AM';

	echo ErrorMessage(array(_('You can only enter eligibility from ').$days[$START_DAY].' '.$START_HOUR.':'.$START_MINUTE.' '.$START_M.' '._('to').' '.$days[$END_DAY].' '.$END_HOUR.':'.$END_MINUTE.' '.$END_M),'error');
}
else
{
	DrawHeader("<A HREF=Modules.php?modname=$_REQUEST[modname]&modfunc=gradebook>"._('Use Gradebook Grades')."</A>",'<INPUT type=submit value='._('Save').'>');

	$LO_columns = array('FULL_NAME'=>_('Student'),'STUDENT_ID'=>_('Student ID'),'GRADE_ID'=>_('Grade')) + $columns;
	ListOutput($stu_RET,$LO_columns,_('Student'),_('Students'));
	echo '<CENTER><INPUT type=submit value='._('Save').'></CENTER>';
}
echo "</FORM>";

function makeRadio($value,$title)
{	global $THIS_RET,$current_RET;
	
	if((isset($current_RET[$THIS_RET['STUDENT_ID']][1]['ELIGIBILITY_CODE']) && $current_RET[$THIS_RET['STUDENT_ID']][1]['ELIGIBILITY_CODE']==$title) || ($title=='PASSING' && !$current_RET[$THIS_RET['STUDENT_ID']][1]['ELIGIBILITY_CODE']))
		return "<INPUT type=radio name=values[".$THIS_RET['STUDENT_ID']."] value='$title' CHECKED>";
	else
		return "<INPUT type=radio name=values[".$THIS_RET['STUDENT_ID']."] value='$title'>";
}

?>